import { Component, EventEmitter, Input, OnChanges, Output, SimpleChanges } from "@angular/core";
import { DateTimeService } from "@app/shared/common/timing/date-time.service";
import { DateTimeHelper } from "@shared/helpers/DateTimeHelper";
import { DateTime } from "luxon";


@Component({
    selector: 'common-date',
    templateUrl: './date-ctl.component.html'
})
export class DateCtlComponent implements OnChanges{

    @Input() time:string;
    @Input() name:string
    @Input() needInit:boolean
    @Input() initReference:string   //参照系，是否是第一次数
    @Input() disabledData:boolean

    @Output() timeChange = new EventEmitter<string>();
    dateTime?:DateTime
    modelDate:string
    isInitTime:boolean = true;
    constructor(private _dateTimeService:DateTimeService){

    }
 
    ngOnChanges(changes: SimpleChanges): void {

        if (changes['time']){
            if (!this.isInitTime) return;
            if (!this.time) {
                if (!this.initReference && this.needInit) {
                    let nowTime = this._dateTimeService.getDate();
                    this.dateTime = nowTime;
                    this.time = DateTimeHelper.ToUnixTimeByMilliseconds(this.dateTime).toString();
                }
            }
            else {
                this.dateTime =  DateTimeHelper.FromUnixTimestampByMilliseconds(Number(this.time));
            }
        }
    }

    dateTimeChange(){
        if (!this.dateTime){
            this.time = '';
            this.isInitTime = false;
            this.timeChange.emit(this.time);
            return;
        }
        //let newTime = this._dateTimeService.toUtcDate(this.dateTime).toMillis().toString();
        let newTime= DateTimeHelper.ToUnixTimeByMilliseconds(this.dateTime).toString();
        this.isInitTime = false;
        this.timeChange.emit(newTime);
        this.time = newTime;
    }
}